**************************************************************************
*																		*
*			Campaign Panel Book Chapter 5 - Mobilization				*
*																		*
*************************************************************************


   
*******************************************************************************
** I.1 Generate Turnout-Variables
*******************************************************************************   

fre kp1_170 kp2_170 kp3_170 kp4_170 kp5_170 kp6_170 kp7_180


* Value range from 0 to 1 (Postal voters = 1)
foreach welle of numlist 1/6 {
recode kp`welle'_170 (1 6=1) (2=0.75) (3=0.5) (4=0.25) (5=0) (else=.), gen(wwbt`welle')  
   }
   
tab kp3_170 wwbt3, m
   
gen wwbt7=1 if kp7_180==1 | kp7_180==6
replace wwbt7=0 if kp7_180==2 

tab wwbt7 kp7_180, m

* Integrate postal voters: after postal vote constant to 1
foreach welle of numlist 4/7 {  
replace wwbt`welle' = 1 if kp3_170==6
}

foreach welle of numlist 5/7 {  
replace wwbt`welle' = 1 if kp4_170==6
}

foreach welle of numlist 6/7 {  
replace wwbt`welle' = 1 if kp5_170==6
}
   
foreach welle of numlist 7/7 {  
replace wwbt`welle' = 1 if kp6_170==6
}   
   


*******************************************************************************
** I.2 Generate variable which indicates whether a respondent possessed a stable
* turnout intention throughout the campaign panel
*******************************************************************************

egen sdwwbt = rowsd(wwbt?)
gen stable = 0 if sdwwbt!=.
replace stable = 1 if sdwwbt==0
fre stable  

**********************************************************
** Case Selection
***********************************************************

* two valid observations for turnout intention
egen misswbt = rowmiss(wwbt?)

gen twovalid = 0
replace twovalid = 1 if misswbt<=5  //minimum two valid observations


lab var twovalid "Zwei gültige Beobachtungen Wahlbeteiligungsabsicht"
fre twovalid

* 7 valid observations
gen allvalid = 0
replace allvalid = 1 if misswbt==0

fre allvalid
lab var allvalid "7 gültige Beobcahtungen Wahlbeteiligungsabsicht"

* participated in 7 waves
fre n_teilnahmen



****************************************************************************
** Dynamic Types Turnout
*****************************************************************************

/* Increasing and decreasing trends, the code is probably 
valid only for respondents with 7 valid observations
*/

/* We do not need main diagonal percentages. To figure out whether observations 
are perfectly stable or not we can use the variable "stable", which was calculated
via rwosd. This variable has the advantage of being able to deal with 
complex missing patterns. Much more efficient. */


* Create ID-Variables for intraindividual regression coefficients
egen id = group(lfdn) if twovalid==1	//minimum two valid observations

fre id if twovalid==0

* Reduce size of dataset to include relevant variables only
keep lfdn wwbt* stable twovalid allvalid id


* Transform to long format
reshape long wwbt, i(lfdn) j(welle)

* Create target variable for coefficients of intraindividual regression
gen rohkoeff = . 

* Loop over all respondents to calculate regression: 
* Fill target variable with values
sum id
forvalues num=1/`r(max)' {	
quietly reg wwbt welle ///
	if id==`num'
replace rohkoeff = _b[welle] ///
	if id==`num'
}

sum rohkoeff


* Transform back into wide format and generate types
reshape wide

* Types
lab define typ ///
	-1 "drecreasing" ///
	0 "stable" ///
	1 "increasing", replace
	
tab rohkoeff stable, m	

gen wwbttyp = -1 if rohkoeff<0 & stable==0
replace wwbttyp = 0 if rohkoeff==0 & stable==1
replace wwbttyp = 1 if rohkoeff>0 & stable==0


lab val wwbttyp typ

fre wwbttyp	//449 missings fluctuation + less than two observations
list wwbt? rohkoeff stable wwbttyp
tab wwbttyp stable, m //if 

lab var wwbttyp "Individual trend turnout"
	
	
*---------------------------------------------
* Keep relevant variables + save	
*----------------------------------------------

keep lfdn wwbt? twovalid allvalid wwbttyp 

save "${path}\Reproducible Do-Files\Chapter 5\Data\mobilization13.dta", replace	

